package cn.tedu.july5;

import cn.tedu.kit.JDBCUtils;

import java.sql.*;
import java.util.Scanner;

/**
 * @author :Colin
 * @version 创建时间: 2021/7/5 9:28
 */
public class TestUser {
    public static void main(String[] args) throws Exception {
        System.out.println("用户名");
        String n = new Scanner(System.in).nextLine();
        System.out.println("密码");
        String p = new Scanner(System.in).nextLine();
        //method(n,p);
        methodsql(n,p);
        //methoddelete(n,p);
    }

    private static void methoddelete(String n, String p) throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        String url="jdbc:mysql:///colinbase?characterEncoding=utf8";
        Connection conn = DriverManager.getConnection(url, "root", "root");
        String sql="delete from user where name=? and password=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1,n);
        ps.setString(2,p);
        int i = ps.executeUpdate();
        System.out.println("删除成功");
        ps.close();
        conn.close();


    }

    private static void methodsql(String n, String p)  {
        Connection conn = null;
        Statement st=null;
        ResultSet rs=null;

        try {
            conn = JDBCUtils.getConnection();
            //3.获取传输器
            st = conn.createStatement();//不安全，sql攻击，jack’#

            //4.执行sql

            String sql="select * from user where name ='"+n+"' and password ='"+p+"'";

             rs = st.executeQuery(sql);

            //5.解析结果集
            if(rs.next()){
                System.out.println("succeed");

            }else{
                System.out.println("fail");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{//6.释放资源
            JDBCUtils.close(rs,st,conn);
        }

    }



    private static void method(String n,String p) throws Exception {
        //1.注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        //String url="jdbc:mysql://localhost:3306/colinbase?characterEncoding=utf8";
        //2.获取链接
        String url="jdbc:mysql:///colinbase?characterEncoding=utf8";
        Connection conn = DriverManager.getConnection(url,"root","root");
        //3.获取传输器
        //Statement st = conn.createStatement();//不安全，sql攻击，jack’#

        //4.执行sql
        //String sql="select * from user where name ='jack' and password ='123'";
        //String sql="select * from user where name ='"+n+"' and password ='"+p+"'";

        //SQL骨架，?是占位符
        String sql="select * from user where name =? and password =?";

        //PreparedStatement把sql骨架和参数分开发送给数据的
        PreparedStatement ps= conn.prepareStatement(sql);
        //ResultSet rs = st.executeQuery(sql);

        //给SQL设置参数--指定要给哪个问号赋值
        ps.setString(1,n);
        ps.setString(2,p);
        ResultSet rs = ps.executeQuery();


        //5.解析结果集
        if(rs.next()){
            System.out.println("succeed");

        }else{
            System.out.println("fail");
        }
        //6.释放资源
        rs.close();
        ps.close();
        conn.close();

    }

}
